<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>




     <div class="am-cf am-padding">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">微店管理</strong> / <small>商品列表</small></div>
      </div>

      <div class="am-g">
        <div class="am-u-sm-12">
	        <form class="am-form-inline" role="form">
			  <div class="am-form-group">
			    <input type="text" class="am-form-field" placeholder="商品标题" id="catNewName">
			  </div>
			
			  <div class="am-form-group">
			    <input type="number" class="am-form-field" placeholder="商品链接" id="catNewShowseq">
			  </div>
			  
			  <div class="am-form-group">
			    <input type="number" class="am-form-field" placeholder="优惠券地址" id="catNewShowseq">
			  </div>				
			
			  <button type="button" id="submitCat" name="submitCat" class="am-btn am-btn-default">增加</button>
			</form>
	        
              
          <table class="am-table am-table-bd am-table-striped admin-content-table" id="tableCat">
            <thead>
            <tr>
              <th>ID</th><th>商品图片</th><th>商品标题</th><th>原价</th><th>优惠券金额</th><th>优惠券剩余数</th><th>优惠券结束时间</th><th>是否推荐</th><th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${pageinfo.list}" var="c" varStatus="status">
            <tr id="tr_${c.id}">
            	<td>${c.id}</td>
            	<td><img src="${c.itempicurl}_50x50.jpg" class="am-circle"  width="50" height="50" /></td>
            	<td>${c.itemtitle}</td>
            	<td>${c.itemprice}</td>
            	<td>${c.couponmoney}</td>
            	<td>${c.couponLeft}</td>
            	<td>${c.couponendtime}</td>
            	<td><input id="checkbox_${c.id}" type="checkbox" onclick="setItemPromt(${c.id});" <c:choose><c:when test="${c.ispromotion == 'Y'}"> checked </c:when> </c:choose> /> </td>
            	<td><input type="button" class=".am-btn-xs" value="删除" id="delItem_${c.id}" onclick="delItem(${c.id})" /></td>             	           
            </tr>    
            </c:forEach>
                
            </tbody>
          </table>
          
		  <ul data-am-widget="pagination"
      class="am-pagination am-pagination-select"
      >
	
	
      <li class="am-pagination-prev ">
      	<c:if test="${pageinfo.pageNumber>1}">
        	<a href="#" onclick="toPage(${pageinfo.pageNumber-1});" class="">上一页</a>\
       </c:if>
      </li>


        <li class="am-pagination-select">
          <select onchange="toPage(this.selectedIndex+1)">
          <c:forEach var="i" begin="1" end="${pageinfo.totalPage}">
   			<option id="option_${i}"
   				<c:if test="${pageinfo.pageNumber == i}">selected</c:if>    				
   				class="">${i}/${pageinfo.totalPage}
		  </c:forEach>              
          </select>
        </li>


      <li class="am-pagination-next ">
      <c:if test="${pageinfo.pageNumber<pageinfo.totalPage}">
        <a href="#" onclick="toPage(${pageinfo.pageNumber+1});" class="">下一页</a>
        </c:if>
      </li>

  </ul>
    
      </div>

      <div class="am-g">
      	<div class="am-u-sm-12">
      	</div>
      </div>



<script>
function toPage(pageNum){
	console.log("toPage:"+pageNum);
	$.get("/admin/item/",
			{pagenum:pageNum,pagesize:${pageinfo.pageSize}},
			function(result){
				if (result.status == 1){
					$("#mainContent").html("ok");
				}
				$("#mainContent").html(result);
	
	});
}
function setItemPromt(id){
	var checked = $("#checkbox_"+id).attr("checked");
	console.log(checked);
	var ispromt = 0;
	if (checked == "checked") ispromt = 1; 
	  $.post("/admin/doItemSetPromt/",{id:id,ispromt:ispromt},function(result){
		    
		  });
}
function delItem(id){
	 $.post("/admin/doItemDel/",{id:id},function(result){
		   if( result.status == 1){
			   $("#tr_"+id).remove();
		   } 
	  });
}
$(document).ready(function(){

	$("#submitCat").click(function(){
		$.post("/admin/doCatAdd/",
				{catname:$("#catNewName").val(),catseqnum:$("#catNewShowseq").val()},
				function(result){
		    if(result.status == 1){
		    	var newrow = '<tr id="tr_'+result.data.id+'"><td>'+result.data.id+'</td><td>'+result.data.name+'</td><td><input id="checkbox_'+result.data.id+'" type="checkbox" onclick="setCatShow('+result.data.id+');" ';
		    	if( result.data.isshow === "Y"){
		    		newrow += " checked ";
		    	}
		    	newrow += '/> </td> <td>' + result.data.showseq + '</td><td>' + result.data.type + '</td>';
		    	newrow += '<td><input type="button" class=".am-btn-xs" value="删除" id="delCat_'+result.data.id+'" onclick="delCat('+result.data.id+')" /></tr>';
		    	$("#tableCat").append(newrow);
		    }
		  });
		
		return false;
	})
	
});
</script>
    